<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class AdminCoupon extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_coupon';

    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'status_text',
        'type_text'
    ];
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 状态
     */
    public function getStatusTextAttr($value,$data){
        if(in_array($data['status'],[-1,0,1])){
            $status = [-1=>'已删除',0=>'未发布',1=>'已发布'];
            $s      = $status[$data['status']];
        }
        return !empty($s)?$s:'';
    }


    /**
     * User: chenniang
     * Date: 2019-10-24 15:35
     * @return void
     * descption:转数据
     */
    public function getFullAttr($value){

        return floor($value);

    }

    /**
     * User: chenniang
     * Date: 2019-10-24 15:35
     * @return void
     * descption:转数据
     */
    public function getReduceAttr($value){

        return floor($value);
    }
    /**
     * @param $value
     * @param $data
     * @return mixed
     * 类型
     */
    public function getTypeTextAttr($value,$data){
        $type = [1=>'线上',2=>'线下'];
        return $type[$data['type']];
    }

    /**
     * @param $value
     * @param $data
     * @return false|string
     * 到期时间
     */
    public function getEndTimeAttr($value,$data){

        if(!empty($data['end_time'])) {

            return date('Y-m-d H:i:s', $data['end_time']);
        }

    }

    /**
     * @param $value
     * @param $data
     * @return false|string
     * 开始时间
     */
    public function getStartTimeAttr($value,$data){

        if(!empty($data['start_time'])&&is_numeric($data['start_time'])){

            return date('Y-m-d H:i:s',$data['start_time']);
        }

    }
    /**
     * @param $value
     * @param $data
     * @return false|string
     * 创建时间
     */
    public function getCreateTimeAttr($value,$data){
        return date('Y-m-d H:i:s',$data['create_time']);
    }

    /**
     * @param $value
     * @return string
     * 修改器 limitgoods
     */
    public function setLimitGoodsAttr($value)
    {
        $value= !empty($value)?implode(',',$value):'';
        return $value;
    }

    /**
     * @param $value
     * @return string
     * 修改器 limitcate
     */
    public function setLimitCateAttr($value)
    {
        $value= !empty($value)?implode(',',$value):'';
        return $value;
    }
    /**
     * @param $value
     * @return string
     * 修改器 endtime
     */
    public function setEndTimeAttr($value)
    {
        $value= !is_numeric($value)?strtotime($value):$value;
        return $value;
    }
    /**
     * @param $dis
     * @param int $page
     * @return array
     * @throws \think\exception\DbException
     * 福报列表
     */
    public function couponList($dis,$page=10){

        $data = self::where($dis)->order('top desc')->paginate($page)->toArray();

        return $data;
    }

    /**
     * 修改福包
     */

    public function couponUpdate($dis,$data){

        $coupon = AdminCoupon::where($dis)->find();

        $data['update_time']= time();

        $res = $coupon->save($data);

        return $res;
    }

    /**
     *
     * 添加福包
     */

    public function couponAdd($data){

        $data['create_time'] = time();
        //限用商品
        $data['limit_goods'] = !empty($data['limit_goods'])?implode(',',$data['limit_goods']):'';
        //限用分类
        $data['limit_cate']  = !empty($data['limit_cate'])?implode(',',$data['limit_cate']):'';

        $data['status']      = 1;

        $res = $this->insert($data);

        return $res;
    }

    /**
     * 获取福包详情
     */

    public function couponInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;

    }




}